﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.OleDb;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;


namespace WindowsFormsApplication2
{
    public class WynikZapytania
    {
        public int kolumny;
        public int wersy;
        List<string> L = new List<string>();

        internal WynikZapytania(OleDbDataReader r)
        {
            kolumny = r.FieldCount;
            wersy = 0;
            for (int i = 0; i < kolumny; i++) L.Add(r.GetName(i));
            while (r.Read())
            {
                for (int i = 0; i < kolumny; i++) L.Add(r.GetValue(i).ToString());
                wersy++;
            }
        }

        public string GetInfo(int x, int y)
        {
            return L[x * kolumny + y];
        }

    }

    class BazaDaych
    {
        public static OleDbConnection con;



        public static void Inicjalizacja()
        {
            Inicjalizacja("baza.mdb");

        }

        public static void Inicjalizacja(string nazwapliku)
        {
            try
            {
                con = new OleDbConnection("Provider=Microsoft.JET.OLEDB.4.0;" + "data source=" + nazwapliku);
                con.Open();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                con.Close();
            }

        }

        public static WynikZapytania WykonajZWynikiem(string kwerenda)
        {
            OleDbDataReader wyn = null;
            WynikZapytania w = null;
            try
            {

                con.Open();

                OleDbCommand cmd = new OleDbCommand(kwerenda, con);
                wyn = cmd.ExecuteReader();
                w = new WynikZapytania(wyn);
                con.Close();

            }
            catch (Exception ex) { throw ex; }
            return w;
        }

        public static void Wykonaj(string kwerenda)
        {


            try
            {

                con.Open();
                OleDbCommand cmd = new OleDbCommand(kwerenda, con);
                cmd.ExecuteScalar();
                con.Close();

            }
            catch (Exception ex) { throw ex; }

        }


    }


    class Operacje
    {
        public static void ViewUpdate(DataGridView d, WynikZapytania w)
        {
            d.Columns.Clear();
            d.Rows.Clear();

            for (int i = 0; i < w.kolumny; i++)
            {

                DataGridViewColumn col = new DataGridViewColumn();
                col.Name = w.GetInfo(0, i);
                col.ValueType = typeof(string);
                col.CellTemplate = new DataGridViewTextBoxCell();
                col.HeaderText = w.GetInfo(0, i);
                col.Visible = true;
                col.AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
                d.Columns.Add(col);
            }

            for (int i = 1; i <= w.wersy; i++)
            {
                int x = d.Rows.Add();
                for (int j = 0; j < w.kolumny; j++)
                    d.Rows[x].Cells[j].Value = w.GetInfo(i, j);
            }
            d.Update();

        }

        public static List<string> Parsowanie(string s)
        {
            List<string> L = new List<string>();
            string temp = "";
            foreach (char c in s) {
                if (c!=',') temp += c;
                else { L.Add(temp.Trim()); temp = "";  }
            }
            L.Add(temp);
            return L;
        }
    }
}
